package com.wjw.java.collection.generic;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * 泛型案例
 * @param <T>
 */
public class Dao<T> {
    private Map<String,T> map = new HashMap<>();

    /**
     * 新增
     * @param key
     * @param value
     */
    public void insert(String key,T value){
        this.map.put(key,value);
    }

    /**
     * 根据map的Key,获取value,
     * @param key
     * @return
     */
    public T getValue(String key){
        return this.map.get(key);
    }

    /**
     * 获取所有的values
     * @return
     */
    List<T> getValues(){
        List<T> list = new ArrayList<>();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            list.add(getValue(key));
        }
        return list;
    }

    public void delete(String key){
        this.map.remove(key);
    }

    @Test
    public void junitTest(){
        Dao<User> userDao = new Dao<>();
        userDao.insert("001",new User("张三",18));
        userDao.insert("002",new User("李四",19));
        userDao.insert("003",new User("王五",20));
        System.out.println(userDao);

        User user = userDao.getValue("001");
        System.out.println(user);
        System.out.println("--------------------------");

        List<User> userList = userDao.getValues();
        System.out.println(userList);
        System.out.println("--------------------------");

        userDao.delete("001");
        List<User> userList2 = userDao.getValues();
        System.out.println(userList2);
    }
}

class User{
    private String name;
    private Integer age;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "\n User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}